package com.xyazm.order.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xyazm.common.dao.BaseDAO;
import com.xyazm.order.domain.entity.OrderDeliveryDetailDO;
import com.xyazm.order.domain.param.AdjustDeliveryAddressParam;
import com.xyazm.order.mapper.OrderDeliveryDetailMapper;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.Objects;


/**
 * 订单配送信息
 * @author xuyou
 * 2022/5/14 0014 22:52
*/
@Repository
public class OrderDeliveryDetailDAO extends BaseDAO<OrderDeliveryDetailMapper, OrderDeliveryDetailDO> {


    /**
     * 根据订单号查询订单配送信息
     * @param orderId
     * @return
     */
    public OrderDeliveryDetailDO getByOrderId(String orderId) {
        LambdaQueryWrapper<OrderDeliveryDetailDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderDeliveryDetailDO::getOrderId,orderId);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 更新配送地址信息
     * @author xuyou
     * 2022/6/21 0021 22:51
    */
    public boolean updateDeliveryAddress(Long id, Integer modifyAddressCount, AdjustDeliveryAddressParam param) {
        LambdaUpdateWrapper<OrderDeliveryDetailDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(StringUtils.isNotBlank(param.getProvince()), OrderDeliveryDetailDO::getProvince,param.getProvince())
                .set(StringUtils.isNotBlank(param.getCity()), OrderDeliveryDetailDO::getCity,param.getCity())
                .set(StringUtils.isNotBlank(param.getArea()), OrderDeliveryDetailDO::getArea,param.getArea())
                .set(StringUtils.isNotBlank(param.getStreet()), OrderDeliveryDetailDO::getStreet,param.getStreet())
                .set(StringUtils.isNotBlank(param.getDetailAddress()), OrderDeliveryDetailDO::getDetailAddress,param.getDetailAddress())
                .set(Objects.nonNull(param.getLat()), OrderDeliveryDetailDO::getLat,param.getLat())
                .set(Objects.nonNull(param.getLon()), OrderDeliveryDetailDO::getLon,param.getLon())
                .set(OrderDeliveryDetailDO::getModifyAddressCount, modifyAddressCount + 1)
                .eq(OrderDeliveryDetailDO::getId, id);
        return update(updateWrapper);
    }


    /**
     * 更新出库时间
     * @author xuyou
     * 2022/7/21 0021 22:17
     * @param id
     * @param outStockTime
     */
    public void updateOutStockTime(Long id, Date outStockTime) {
        LambdaUpdateWrapper<OrderDeliveryDetailDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(OrderDeliveryDetailDO::getOutStockTime,outStockTime)
                .eq(OrderDeliveryDetailDO::getId,id);
        update(updateWrapper);
    }

    /**
     * 更新配送员信息
     * @author xuyou
     * 2022/7/21 0021 22:19
     * @param id
     * @param delivererNo
     * @param delivererName
     * @param delivererPhone
    */
    public void updateDeliverer(Long id, String delivererNo, String delivererName, String delivererPhone) {
        LambdaUpdateWrapper<OrderDeliveryDetailDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(OrderDeliveryDetailDO::getDelivererNo,delivererNo)
                .set(OrderDeliveryDetailDO::getDelivererName,delivererName)
                .set(OrderDeliveryDetailDO::getDelivererPhone,delivererPhone)
                .eq(OrderDeliveryDetailDO::getId,id);
        update(updateWrapper);
    }


    /**
     * 更新签收时间
     * @author xuyou
     * 2022/7/21 0021 22:16
     * @param id
     * @param signedTime
    */
    public void updateSignedTime(Long id, Date signedTime) {
        LambdaUpdateWrapper<OrderDeliveryDetailDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(OrderDeliveryDetailDO::getSignedTime, signedTime)
                .eq(OrderDeliveryDetailDO::getId, id);
        update(updateWrapper);
    }

}
